package ess.hardware;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.plaf.InsetsUIResource;

import ess.control.Direction;

public class FloorButtonPanel extends JPanel implements ActionListener {
	int numOfFloors;
	int width;
	int height;
	FloorOperationEventListener listener;
	
	Vector upButtons;
	Vector downButtons;
	Vector obstacleButtons;
	
	Color defaultBgColor = null; 
	Color selectedBgColor = Color.YELLOW;
	
	public void addFloorOperationEventListener(FloorOperationEventListener l)
	{
		listener = l;
	}

	public Dimension getPreferredSize(){
		return new Dimension(width,height);
	}
	
	public FloorButtonPanel(int numOfFloors, int width, int height){
		
		this.numOfFloors = numOfFloors;
		this.width = width;
		this.height = height;
		
		upButtons = new Vector();
		downButtons = new Vector();;
		
		GridLayout layout = new GridLayout(numOfFloors, 2);
	    setLayout(layout);

	    JButton button = null;
		for (int i=0; i<numOfFloors;i++) {
			
			button = new JButton("UP");
			button.setMargin(new InsetsUIResource(0, 0, 0, 0));
			button.setSize(width, width);
			button.addActionListener(this);
			if(i==0) button.setEnabled(false);
		
			add(button);
			upButtons.add(button);
			
			button = new JButton("DOWN");
			button.setMargin(new InsetsUIResource(0, 0, 0, 0));
			button.setSize(width, width);
			button.addActionListener(this);
			if(i==numOfFloors-1) button.setEnabled(false);
			
			add(button);
			downButtons.add(button);
		}
		defaultBgColor = button.getBackground();
	}
	
	
	
	JButton getUpButton(int floor){
		return (JButton)upButtons.elementAt(floor-1);
	}
	JButton getDownButton(int floor){
		return (JButton)downButtons.elementAt(floor-1);
	}
	JButton getObstacleButton(int floor){
		return (JButton)obstacleButtons.elementAt(floor-1);
	}
	
	
	public void actionPerformed(ActionEvent e) {
		if(listener == null) return;
		
		JButton btn = (JButton) e.getSource();
		
		int operationType = FloorOperationEvent.TYPE_CALL;
		int floor = numOfFloors - btn.getLocation().y/(height/numOfFloors);
		int operationValue = 0;
		
		System.out.println("button x:"+btn.getLocation().x+", y:"+btn.getLocation().y);
		
		if(btn.getText().equals("UP")){
			if(defaultBgColor == Color.YELLOW){
				return;
			}
			operationValue = FloorOperationEvent.VALUE_UP;
			/*if(defaultBgColor == btn.getBackground()){
				btn.setBackground(Color.YELLOW);
			}*/
		}
		else if(btn.getText().equals("DOWN")){
			if(defaultBgColor == Color.YELLOW){
				return;
			}
			operationValue = FloorOperationEvent.VALUE_DOWN;
			/*if(defaultBgColor == btn.getBackground()){
				btn.setBackground(Color.YELLOW);
			}*/
			
			
		}

		//System.out.println("actionPerformed floor("+floor+"), type("+operationType+"), value("+operationValue+")");
		
		repaint();
		
		listener.operationOccured(new FloorOperationEvent(floor, operationType, operationValue));
		
	}
	public void turnOnButton(int floor, int direction) {
		JButton btn;
		
		if(direction == Direction.UP){
			btn = (JButton) upButtons.elementAt(7-floor);
		} else {
			btn = (JButton) downButtons.elementAt(7-floor);
		}
		
		btn.setBackground(Color.YELLOW);
		
	}
	public void turnOffButton(int floor, int direction){
		JButton btn;
		if(direction == Direction.UP){
			btn = (JButton) upButtons.elementAt(7-floor);
		} else {
			btn = (JButton) downButtons.elementAt(7-floor);
		}
		System.out.println("button x:"+btn.getLocation().x+", y:"+btn.getLocation().y);
		btn.setBackground(defaultBgColor);
	}
}
